home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17296 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: ix.netcom.com!als-il4-33
  2. From: stefmit@ix.netcom.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Expression tree evaluation
  5. Date: 14 Apr 1996 16:22:36 GMT
  6. Organization: Netcom
  7. Message-ID: <4kr8oc$7bj@dfw-ixnews8.ix.netcom.com>
  8. References: <4kp8ja$s6p@cloner2.ix.netcom.com>
  9. NNTP-Posting-Host: als-il4-33.ix.netcom.com
  10. X-NETCOM-Date: Sun Apr 14 11:22:36 AM CDT 1996
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12.  
  13. In article <4kp8ja$s6p@cloner2.ix.netcom.com>, stefmit@ix.netcom.com wrote:
  14. >Could anybody tell me what's wrong with this (I am surely missing something):
  15. >
  16. >float ExprTree :: evaluate () const
  17. >{    if (root != 0)
  18. >        evaluateSub (root); }
  19. >
  20. >float ExprTree :: evaluateSub (ExprTreeNode *p) const
  21. >{    float result, l, r;
  22. >    if (isdigit (p -> element))
  23. >        result = ((p -> element) - '0');
  24. >    else    {    l = evaluateSub (p -> left);
  25. >            r = evaluateSub (p -> right); }
  26. >    switch (p -> element) {
  27. >        case '+' : result = l+r; break;
  28. >        case '-' : result = l-r; break;
  29. >        case '*' : result = l*r; break;
  30. >        case '/' : result = l/r; break;
  31. >        default : break;}
  32. >    return (result); }
  33. >
  34. >I am obviously trying to evaluate an expression, I "watched" l, r and result 
  35. >and they show correct values (while stepping through the program), but the 
  36. >program crashes with "stack underflow". Could anybody help, please? Thanks.
  37.  
  38.  
  39. Sorry, it's me again - I missed the return in the main evaluate. Stupid, isn't 
  40. it? I looked so hard at the Sub one, that I missed the main one. Sorry again.
  41.